package com.nova.edc.service.energy.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.nova.edc.model.system.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.nova.edc.model.energy.CollectData;
import com.nova.edc.model.energy.Energy;
import com.nova.edc.service.energy.EnergyService;
import com.nova.edc.service.system.CollectAreaItemService;
import com.nova.frame.dao.parsing.SQLParser;
import com.nova.frame.utils.JdbcUtils;
import com.nova.module.sql.SqlBuffer;

public class EnergyServiceImpl implements EnergyService {

	@Autowired
	private CollectAreaItemService novaCollectAreaItemService;

	@Override
	public void updateLightUseHour(List<AreaPoint> areaPoints,
			Integer areaId, String startTime, String endTime) {
		String sql = "select cd.GatewayID,cd.SensorID,cd.ChannelID,sum(cd.ActiveCount) useminute from t_collectdata cd"
				+ " where cd.AreaId = ?"
				+ " and cd.ActiveState = 1"
				+ " and cd.ActiveTime > ?"
				+ " and cd.ActiveTime < ?"
				+ " group by cd.GatewayID,cd.SensorID,cd.ChannelID";
		List<CollectData> result = JdbcUtils.query(CollectData.class, sql,
				areaId, startTime, endTime);
		for (int i = 0; i < areaPoints.size(); i++) {
			AreaPoint areaPoint = areaPoints.get(i);
			for (int j = 0; j < result.size(); j++) {
				CollectData collectData = result.get(j);
				if (collectData.equals(areaPoint)) {
					areaPoint.setUseminute(collectData.getUseminute());
					break;
				}
			}
			if (areaPoint.getUseminute() == null) {
				areaPoint.setUseminute(0);
			}
		}

	}

	@Override
	public int updateAirUseHour(List<CollectAreaItemAir> areaItems,
			int companyCode, String startTime, String endTime) {
		String sql = "select cd.GatewayID,cd.SensorID,cd.ChannelID,sum(cd.ActiveCount) useminute from t_collectdata cd"
				+ " where cd.CompanyCode = ?"
				+ " and cd.ActiveState = 1"
				+ " and cd.ActiveTime > ?"
				+ " and cd.ActiveTime < ?"
				+ " group by cd.GatewayID,cd.SensorID,cd.ChannelID";
		List<CollectData> result = JdbcUtils.query(CollectData.class, sql,
				companyCode, startTime, endTime);
		int alluseminute = 0; // 按公司获取使用时间
		for (int j = 0; j < result.size(); j++) {
			alluseminute += result.get(j).getUseminute();
		}
		for (int i = 0; i < areaItems.size(); i++) {
			CollectAreaItemAir areaItem = areaItems.get(i);
			for (int j = 0; j < result.size(); j++) {
				CollectData collectData = result.get(j);
				if (collectData.equals(areaItem)) {
					areaItem.setUseminute(collectData.getUseminute());
					break;
				}
			}
			if (areaItem.getUseminute() == null) {
				areaItem.setUseminute(0);
			}
		}
		return alluseminute;

	}

	@Override
	public void updateLightUseHour(List<CollectAreaItem> areaItems,
			List<Integer> companyCodes, String startTime, String endTime) {
		String sql = "select cd.GatewayID,cd.SensorID,cd.ChannelID,sum(cd.ActiveCount) useminute from t_collectdata cd"
				+ " where cd.CompanyCode in (#)"
				+ " and cd.ActiveState = 1"
				+ " and cd.ActiveTime > ?"
				+ " and cd.ActiveTime < ?"
				+ " group by cd.GatewayID,cd.SensorID,cd.ChannelID";
		List<CollectData> result = JdbcUtils.query(CollectData.class,
				sql.replace("#", StringUtils.join(companyCodes, ",")),
				startTime, endTime);
		for (int i = 0; i < areaItems.size(); i++) {
			CollectAreaItem areaItem = areaItems.get(i);
			for (int j = 0; j < result.size(); j++) {
				CollectData collectData = result.get(j);
				if (collectData.equals(areaItem)) {
					areaItem.setUseminute(collectData.getUseminute());
					break;
				}
			}
			if (areaItem.getUseminute() == null) {
				areaItem.setUseminute(0);
			}
		}

	}

	@Override
	public Energy getEnergyByCompany(int companyCode, String startDate,
			String endDate) {
		SqlBuffer sqlBuffer = new SqlBuffer(
				SQLParser.sql("energySQL.getAllPowerByCompanyCode"), true);
		Energy energy = JdbcUtils.get(Energy.class, sqlBuffer.getSql(),
				companyCode, startDate, endDate);
		return energy;
	}

	@Override
	public Map<String, BigDecimal> getEnergyCost(
			Map<String, BigDecimal> energy, BigDecimal unitPrice) {
		Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
		if (!energy.isEmpty() && unitPrice != null) {
			BigDecimal traCost = energy.get(ModelConstant.TRA_POWER).multiply(
					unitPrice);
			BigDecimal ledCost = energy.get(ModelConstant.LED_POWER).multiply(
					unitPrice);
			map.put(ModelConstant.TRA_COST, traCost);
			map.put(ModelConstant.LED_COST, ledCost);
			map.put(ModelConstant.SAVE_COST, traCost.subtract(ledCost));
		}
		return map;
	}

	@Override
	public List<Map<String, Object>> getEnergyByDate(int companyCode,
			String startDate, String endDate) {
		SqlBuffer sqlBuffer = new SqlBuffer(
				SQLParser.sql("energySQL.getPowerByDate"), true);

		List<Map<String, Object>> result = JdbcUtils.queryMap(
				sqlBuffer.getSelectSql(), companyCode, startDate, endDate);
		return result;
	}

	@Override
	public List<Map<String, Object>> getEnergyByRegion(int companyCode,
			String startDate, String endDate) {
		SqlBuffer sqlBuffer = new SqlBuffer(
				SQLParser.sql("energySQL.getPowerByRegion"), true);
		List<Map<String, Object>> result = JdbcUtils.queryMap(
				sqlBuffer.getSelectSql(), companyCode, startDate, endDate);
		return result;
	}

	@Override
	public List<Map<String, Object>> getAllEnergyByDate(int companyCode,
			String startDate, String endDate) {
		SqlBuffer sqlBuffer = new SqlBuffer(
				SQLParser.sql("energySQL.getAllPowerByDate"), true);
		List<Map<String, Object>> result = JdbcUtils.queryMap(
				sqlBuffer.getSelectSql(), companyCode, startDate, endDate);

		return result;
	}

	@Override
	public List<Map<String, Object>> getUnitEnergyByDate(String companyCode,
			String startDate, String endDate) {
		SqlBuffer sqlBuffer = new SqlBuffer(
				SQLParser.sql("energySQL.getUnitEnergyByDate"), true);
		List<Map<String, Object>> result = JdbcUtils.queryMap(sqlBuffer
				.getSelectSql().replace("#", companyCode), startDate, endDate);
		return result;

	}

	@Override
	public List<Map<String, Object>> getAllNightByDate(String companyCode,
			String startTime, String endTime, String startDate, String endDate) {
		SqlBuffer sqlBuffer = new SqlBuffer(
				SQLParser.sql("energySQL.getAllNightByDate"), true);
		List<Map<String, Object>> result = JdbcUtils.queryMap(sqlBuffer
				.getSelectSql().replace("#", companyCode), startTime, endTime,
				startDate, endDate);
		return result;
	}

	@Override
	public Map<String, BigDecimal> getDetailEnergyByCompany(
			Integer companyCode, String startTime, String endTime) {
		CollectAreaItem collectAreaItem = new CollectAreaItem();
		collectAreaItem.setCompanycode(companyCode);
		List<CollectAreaItem> collectAreaItems = novaCollectAreaItemService
				.findByCompanyCode(collectAreaItem,
						"gatewayid,SensorID,ChannelID", "asc");
//		updateLightUseHour(collectAreaItems, collectAreaItem.getCompanycode(),
//				startTime, endTime);
		Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
		double traPower = 0;
		double ledPower = 0;
		for (CollectAreaItem areaItem : collectAreaItems) {
			traPower = traPower
					+ (areaItem.getTralightPower() * areaItem.getTranum() * areaItem
							.getUseminute());
			ledPower = ledPower
					+ (areaItem.getLedlightPower() * areaItem.getLednum() * areaItem
							.getUseminute());
		}
		map.put(ModelConstant.TRA_POWER, new BigDecimal(traPower / 60000));
		map.put(ModelConstant.LED_POWER, new BigDecimal(ledPower / 60000));
		return map;
	}

	@Override
	public Map<String, BigDecimal> getDetailEnergyByCompany(
			List<Integer> companyCodes, String startTime, String endTime) {
		Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
		double traPower = 0;
		double ledPower = 0;
		List<CollectAreaItem> collectAreaItems = novaCollectAreaItemService
				.findByCompanyCode(companyCodes,
						"gatewayid,SensorID,ChannelID", "asc");
		updateLightUseHour(collectAreaItems, companyCodes, startTime, endTime);

		for (CollectAreaItem areaItem : collectAreaItems) {
			traPower = traPower
					+ (areaItem.getTralightPower() * areaItem.getTranum() * areaItem
							.getUseminute());
			ledPower = ledPower
					+ (areaItem.getLedlightPower() * areaItem.getLednum() * areaItem
							.getUseminute());
		}

		map.put(ModelConstant.TRA_POWER, new BigDecimal(traPower / 60000));
		map.put(ModelConstant.LED_POWER, new BigDecimal(ledPower / 60000));
		return map;
	}

	@Override
	public List<Map<String, Object>> getGatewayDetail(String companyCode,
			String searchDate, String gatewayId) {
		SqlBuffer sqlBuffer = new SqlBuffer(
				SQLParser.sql("energySQL.getGatewayDetail"), true);
		List<Map<String, Object>> result = JdbcUtils.queryMap(
				sqlBuffer.getSelectSql(), gatewayId, searchDate);
		return result;
	}
}
